﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

using EzDesk.DesktopClient.View;
using EzDesk.DesktopClient.ViewModel;
using System.Windows.Controls;
using EzDesk.DesktopClient.ViewContract;

using Microsoft.Practices.Unity.Utility;
using EzDesk.DesktopClient;
using EzDesk.Generic.WPF;
using EzDesk.DesktopClient.Event;


namespace BrowserTest
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            IMClientUnityContainerStatic.RegistViews();
            InitWindow();
            InitEvent();
        }

        private void InitEvent()
        {
            FunctionNavigateEvent.Instance.Subscribe(args => this.FunctionPageViewModel.Navigate(args.Url));
        }


        public FunctionPageViewModel FunctionPageViewModel { get; set; }

        public Window1 MainWindow { get; set; }
        public Window WebWindow
        {
            get
            {
                return FunctionPageViewModel.View as Window;
            }
        }

        private void InitWindow()
        {
            this.MainWindow = new Window1();
            this.MainWindow.Show();
            this.MainWindow.SizeChanged += new SizeChangedEventHandler(window_SizeChanged);
            this.MainWindow.LocationChanged += new EventHandler(
                (o, e) => RefreshPosition()
                );

            this.FunctionPageViewModel= new FunctionPageViewModel();
            WebWindow.Show();
            WebWindow.SizeChanged += new SizeChangedEventHandler(WebWindow_SizeChanged);
            this.FunctionPageViewModel.Navigate(@"C:\Users\Superwolf\Documents\My Dropbox\EZDeskDev\CodePlex\EZDesk-Dev\Core\Clients\DesktopClient\BrowserTest\TestPages\test.html");
            this.MainWindow.URLTbx.Text = this.FunctionPageViewModel.Url;

            RefreshPosition();
        }

        void WebWindow_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            MainWindow.Width = WebWindow.Width;
            RefreshPosition();
        }
        void window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            RefreshPosition();
        }
        private void RefreshPosition()
        {
            this.WebWindow.ToBeTop();
            this.MainWindow.ToBeTop();
            WebWindow.ToBeside(MainWindow, direction: EzDesk.Generic.WindowPostionDirection.Down);
        }
    }
}
